<template>
	<view class="container">
		<button class="event-button" v-for="(event, index) in events" :key="index" @click="sendEventMessage(index + 1)">
			{{ event }}
		</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				events: ['正常', '想睡', '舒服', '到时', '难受'],
				deviceUUID: null
			}
		},
		onLoad(options) {
			if (options.uuid) {
				console.log('call页面收到uuid', options.uuid);
				this.deviceUUID = options.uuid;
			} else {
				console.log('call页面没有收到uuid')
			}
		},
		methods: {
			sendEventMessage(code) {
				const message = {
					msg: 'alert_from_app',
					code: String(code)
				};
				const opts = {
					topic: `LSY/app/${this.deviceUUID}`,
					message: JSON.stringify(message)
				};
				this.$mqttTool.publish(opts).then(res => {
					console.log('MQTT消息发送成功', res);
				}).catch(err => {
					console.error('MQTT消息发送失败', err);
				});
			}
		}
	}
</script>

<style>
	.container {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		height: 100vh;
	}

	.event-button {
		margin: 10px;
		padding: 10px 20px;
		background-color: #007bff;
		color: white;
		border: none;
		border-radius: 5px;
		cursor: pointer;
		font-size: 16px;
	}

	.event-button:hover {
		background-color: #0056b3;
	}
</style>